/*
 * Copyright 2011 John Alström
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package se.elva.slticket;

import se.elva.slticket.dialog.InfoDialog;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.TextView;

/**
 * Sms- och automatbiljett, 1 zon 30 kr 18 kr
 * Sms- och automatbiljett, 2 zoner 45 kr 27 kr 
 * Sms- och automatbiljett, 3 zoner 60 kr 36 kr
 * 
 */
public class SLTicket extends SmsTicket {

	private int ticketPrice = 0;
	private final String FULLPRICE = "H";
	private final String REDUCEDPRICE = "R";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		((CheckBox) findViewById(R.id.CheckBoxZoneA))
				.setOnCheckedChangeListener(priceChangeListener);
		((CheckBox) findViewById(R.id.CheckBoxZoneB))
				.setOnCheckedChangeListener(priceChangeListener);
		((CheckBox) findViewById(R.id.CheckBoxZoneC))
				.setOnCheckedChangeListener(priceChangeListener);

		((RadioButton) findViewById(R.id.RadioButtonHeltPris))
				.setOnCheckedChangeListener(priceChangeListener);
		((RadioButton) findViewById(R.id.RadioButtonReduceratPris))
				.setOnCheckedChangeListener(priceChangeListener);

		((Button) findViewById(R.id.send)).setOnClickListener(mBackListener);
		((Button) findViewById(R.id.cancel))
				.setOnClickListener(mCancelListener);
		
	}

	// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
	// = listeners
	// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

	OnClickListener mCancelListener = new OnClickListener() {
		public void onClick(View v) {
			finish();
		}
	};

	OnCheckedChangeListener priceChangeListener = new OnCheckedChangeListener() {
		public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
			updateTicketPrice();
		}
	};

	OnClickListener mBackListener = new OnClickListener() {
		public void onClick(View v) {
			String price = getPrice();
			String zone = getZone();

			if (!"".equals(zone) && !"".equals(price)) {
				sendSms(getPhoneNumer(), price + " " + zone, SLTicket.this);
			} else {
				showInfoDialog("Fel",getString(R.string.errorZone));
			}
		}
	};

	private void updateTicketPrice() {
		String selectedZones = getZone();
		String priceCategory = getPrice();

		if (priceCategory.equalsIgnoreCase("H") && selectedZones.length() > 0) {
			ticketPrice = 18 + (18 * selectedZones.length());
		} else if (priceCategory.equalsIgnoreCase("R")
				&& selectedZones.length() > 0) {
			ticketPrice = 10 + (10 * selectedZones.length());
		} else {
			ticketPrice = 0;
		}
		String code = "\nKoden " + getTextToSend() + " kommer skickas till " + getPhoneNumer();
		((TextView) findViewById(R.id.Summa)).setText(getString
				(R.string.ticketPrice).replace(":ticketPrice", String.valueOf(ticketPrice))+code);
	}

	private String getZone() {
		String zone = "";
		if (((CheckBox) findViewById(R.id.CheckBoxZoneA)).isChecked()) {
			zone += "A";
		}
		if (((CheckBox) findViewById(R.id.CheckBoxZoneB)).isChecked()) {
			zone += "B";
		}
		if (((CheckBox) findViewById(R.id.CheckBoxZoneC)).isChecked()) {
			zone += "C";
		}
		return zone;
	}

	private String getPrice() {
		if (((RadioButton) findViewById(R.id.RadioButtonHeltPris)).isChecked()) {
			return FULLPRICE;
		} else if (((RadioButton) findViewById(R.id.RadioButtonReduceratPris))
				.isChecked()) {
			return REDUCEDPRICE;
		}
		
		return "";
	}
	
    @Override
    protected Dialog onCreateDialog(int id) {
    	return new AlertDialog.Builder(SLTicket.this)
        .setTitle(R.string.pickCity)
        .setItems(R.array.city, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                saveChoice = which;
                Intent intent = findIntentToStart(SLTicket.this, which);
                startActivity(intent);
                finish();
            }
        })
        .create();
    
    }

    @Override
    protected void onResume() {
        super.onResume();

        SharedPreferences prefs = getPreferences(0); 
        String restoredText = prefs.getString("saveChoice", null);
        if (restoredText != null) {
        	saveChoice = Integer.valueOf(restoredText);
        	Intent intent = findIntentToStart(SLTicket.this, saveChoice,false);
			if (intent != null) {
				startActivity(intent);
				finish();
			}
        }else{
        	showDialog(0);
        }
    }

	@Override
	protected void showInfoDialog(String title, String msg) {
		final InfoDialog info = new InfoDialog(SLTicket.this, msg);
		info.show();
	}

	@Override
	public String getPhoneNumer() {
		return getPrefix()+Constans.PHONENUMBER_STOCKHOLM;
	}

	@Override
	public String getTextToSend() {
		return getPrice() + " " + getZone();
	}


	
}